/**   
* @Title: RecursionTreeUtils.java
* @Package org.xaoyaoyao.cloud.common.utils
* @Description: TODO
* @author xaoyaoyao
* @date Feb 14, 2019 2:04:19 PM
* @version V1.0   
*/
package org.xaoyaoyao.cloud.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.xaoyaoyao.cloud.common.bean.TreeNode;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * @ClassName: RecursionTreeUtils
 * @Description: 递归树
 * @author xaoyaoyao
 * @date Feb 14, 2019 2:04:19 PM
 * 
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class RecursionTreeUtils {

	/**
	 * @Title: getChildTreeNodes
	 * @Description: Gets child tree nodes.
	 * @param list
	 * @param parentId
	 * @return List<TreeNode>
	 */
	public static List<TreeNode> getChildTreeNodes(List<TreeNode> list, Long parentId) {
		List<TreeNode> returnList = new ArrayList<TreeNode>();
		for (TreeNode treeNode : list) {
			if (treeNode.getPid() == null) {
				continue;
			}
			if (Objects.equals(treeNode.getPid(), parentId)) {
				recursionFn(list, treeNode);
				returnList.add(treeNode);
			}
		}
		return returnList;
	}

	/**
	 * @Title: recursionFn
	 * @Description: 递归列表
	 * @param list
	 * @param treeNode
	 * @return void
	 */
	private static void recursionFn(List<TreeNode> list, TreeNode node) {
		List<TreeNode> childList = getChildList(list, node);
		if (StringUtils.isEmpty(childList)) {
			return;
		}
		node.setChildren(childList);
		for (TreeNode tChild : childList) {
			recursionFn(list, tChild);
		}
	}

	/**
	 * @Title: getChildList
	 * @Description: 得到子节点列表
	 * @param list
	 * @param node
	 * @return List<TreeNode>
	 */
	private static List<TreeNode> getChildList(List<TreeNode> list, TreeNode node) {
		List<TreeNode> tList = new ArrayList<TreeNode>();
		for (TreeNode treeNode : list) {
			if (StringUtils.isEmpty(treeNode.getPid())) {
				continue;
			}
			if (Objects.equals(treeNode.getPid(), node.getId())) {
				tList.add(treeNode);
			}
		}
		return tList;
	}
}